<%--
  Created by IntelliJ IDEA.
  User: xh
  Date: 2015/3/2
  Time: 15:44
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<style>
    .ui-jqgrid{border-top:1px solid #e7ebee !important;}
</style>
<input type="hidden" id="selectedIds" value=""/>
<input type="hidden"  id="proInfo_id" value="">
<div class="row" style="padding:2px;margin-bottom: 5px ">
    <div class="col-sm-12 col-md-12 col-lg-12">
        <div >
            <shiro:hasAnyRoles name="wechat">
            <form id="searchForm" method="post" action="">
                <s:if test='dutyNum!="0"'>
                    <a id="ajax_project_btn_add" class="btn btn-default " data-toggle="modal"><i class="fa fa-lg fa-plus"></i> 新建项目信息表</a>
                </s:if>
                <s:else>
                    <i class="fa-fw fa fa-warning"></i>
                    <strong>注意</strong> <span class="label label-info bg-color-red txt-color-white">没有分配部门的用户无法新建</span>
                </s:else>
                <a id="ajax_download_proInfo_datas" class="btn btn-default pull-right" href="javascript:void(0);"> <i class="fa fa-lg fa-hand-o-right"></i> 进入下载中心 </a>
                <a id="ajax_export_proInfo_datas" class="btn btn-default pull-right" href="javascript:void(0);" style="margin-right: 5px;"><i class="fa fa-lg fa-external-link"></i> 项目信息数据导出 </a>
            </form>
            </shiro:hasAnyRoles>
        </div>
    </div>
</div>

<ul id="myTab1" class="nav nav-tabs bordered  ">
    <li class="active">
        <a href="#s1" id="todo" data-toggle="tab"><i class="fa fa-fw fa-lg fa-credit-card"></i> 实施中项目 </a>
    </li>
    <li>
        <a href="#s2" id="finish" data-toggle="tab"><i class="fa fa-fw fa-lg fa-users"></i> 已完成项目 </a>
    </li>
</ul>

<div id="myTabContent1" class="tab-content padding-10 ">

    <div class="tab-pane fade in active " id="s1" style="margin: 10px;">

        <div class="row" id="ajax_project_list_row" class="col-sm-12 col-md-12 col-lg-12">
            <table id="ajax_project_table" class="table table-striped table-bordered table-hover">
            </table>
            <div id="ajax_project_list_page">
            </div>
        </div>

    </div>

    <div class="tab-pane fade in " id="s2" style="margin: 10px">

        <div class="row" id="ajax_projectFin_list_row" class="col-sm-12 col-md-12 col-lg-12">
            <table id="ajax_projectFin_table" class="table table-striped table-bordered table-hover">
            </table>
            <div id="ajax_projectFin_list_page">
            </div>
        </div>
    </div>

</div>


<script type="text/javascript">
    // DO NOT REMOVE : GLOBAL FUNCTIONS!
    pageSetUp();

    function project_reload(){
        loadURL("ajax-project!view.action",$('div#main div#business_data'));
    }

    $("#ajax_project_btn_add").click(function(){
        loadURL("ajax-project!input.action",$('#business_data'));
    });

    //删除
    function fn_delete(id){
        $.SmartMessageBox({
            title : "<i class='fa fa-minus-square txt-color-orangeDark'></i> <span class='txt-color-orangeDark'><strong>删除</strong></span>",
            content : "确定删除该项目信息吗？",
            buttons : '[取消][确定]'
        }, function(ButtonPressed) {
            if (ButtonPressed === "确定") {
                var vActionUrl = "<%=path%>/com/ajax-project!delete.action";
                data={keyId:id};
                ajax_action(vActionUrl,data,function(pdata){
                    _show(pdata);
                });
                jQuery("#ajax_project_table").trigger("reloadGrid");
            }
        });
    }

</script>
<script type="text/javascript">
    $(function(){
        $("#selectedIds").val("");
        load_project_jqGrid();
    });
    function load_project_jqGrid(){
        jQuery("#ajax_project_table").jqGrid({
            url:'ajax-project!list.action',
            datatype: "json",
            colNames:['项目名称',"项目类别",'项目总监',"客户名称","客户联系人",'手机',"所在区域","项目地址","启动时间","操作","id","latitude","longitude"],
            colModel:[
                {name:'name',index:'name', width:120,sortable:true, search:true,
                    searchoptions:{sopt: ["cn"],clearSearch:true}},
                {name:'category',index:'category', width:80,sortable:true,search:true,
                    searchoptions:{sopt: ["cn"],clearSearch:true}},
                {name:'chief',index:'chief', width:80,search:true,
                    searchoptions:{sopt: ["cn"],clearSearch:true}},
                {name:'client',index:'client', width:200,search:true,
                    searchoptions:{sopt: ["cn"],clearSearch:true}},
                {name:'linkman',index:'linkman', width:80,search:false},
                {name:'mobile',index:'mobile', width:100,search:false},
                {name:'area',index:'province', width:120,search:false},
                {name:'address',index:'address', width:150,search:false},
                {name:'start',index:'start', width:80,search:false},
                {name:'act',index:'act', width:210,sortable:false,fixed:true,search:false},
                {name:'id',index:'id',hidden:true,search:false},
                {name:'longitude',index:'longitude',hidden:true,search:false},
                {name:'latitude',index:'latitude',hidden:true,search:false},
            ],
            rowNum : 10,
            rowList:[10,20,30],
            pager : '#ajax_project_list_page',
            sortname : 'start',
            sortorder : "desc",
            gridComplete:function(){
                var ids=$("#ajax_project_table").jqGrid('getDataIDs');
                var selectedIds = $("#selectedIds").val();
                for(var i=0;i<ids.length;i++){
                    var cl=ids[i];
//                    var objIds = $("#selectedIds").val();
//                    $(objIds.split(",")).each(function(i,v){
//                        if(v==cl)$("#ajax_project_table").jqGrid('setSelection',v);
//                    });
                    if (selectedIds != "" && selectedIds != null){
                        var idsArr = selectedIds.split(",");
                        for (var s=0;s<idsArr.length;s++){
                            if (idsArr[s] == cl){
                                $("#ajax_project_table").jqGrid("setSelection", cl);
                            }
                            // break;
                        }
                    }
                    var rowData = $("#ajax_project_table").jqGrid("getRowData",cl);
                    var de="<button class='btn btn-default' data-original-title='查看' onclick=\"fn_project_read('"+cl+"');\"><i class='fa fa-eye'></i>查看</button>"+" ";
                    var dt="<button class='btn btn-default' data-original-title='删除' onclick=\"fn_project_destroy('"+cl+"');\"><i class='fa fa-ban'></i>删除</button>"+" ";
                    var df="<button class='btn btn-default' data-original-title='完成' onclick=\"fn_project_finish('"+cl+"');\"><i class='fa fa-check'></i>完成</button>"+" ";
                    if($("#ajax_project_btn_add").length==0){
                        dt="";
                        df="";
                    }

                    jQuery("#ajax_project_table").jqGrid('setRowData',ids[i],{act:de+dt+df});
                }

                $(".ui-jqgrid-bdiv").css("overflow-x","hidden");
                jqGridStyle();
            },
            onSelectRow: function (rowId, status, e) {
                var str = $("#ajax_project_table").jqGrid('getGridParam','selrow');
                var ids = $("#selectedIds").val();
                if (ids != ""){
                    var idsArr = ids.split(",");
                    var isSelected = false;
                    for (var i=0; i<idsArr.length;i++){
                        if (idsArr[i] == str){
                            isSelected = true;
                            break;
                        }
                    }
                    if (status){
                        if (!isSelected)
                            $("#selectedIds").val(ids + "," +str);
                    }
                    else{
                        if (isSelected) {
                            for (var i = 0; i < idsArr.length; i++) {
                                if (idsArr[i] == str) {
                                    for (var j = i; j < idsArr - 1; j++) {
                                        idsArr[j] = idsArr[j + 1];
                                    }
                                    idsArr.length = idsArr.length - 1;
                                    break;
                                }
                            }
                            $("#selectedIds").val(idsArr.join(","));
                        }
                    }
                }
                else{
                    $("#selectedIds").val(str);
                }

            },
            onSelectAll:function(aRowids, status, e){
                var ids = $("#selectedIds").val();
                if (ids != "") {
                    var idsArr = ids.split(",");
                    for (var i = 0; i < aRowids.length; i++) {
                        var isSelected = false;
                        var index = -1;
                        for (var j = 0; j < idsArr.length; j++) {
                            if (aRowids[i] == idsArr[j]) {
                                isSelected = true;
                                index = j;
                                break;
                            }
                        }
                        if (status) {
                            if (!isSelected) {
                                idsArr.push(aRowids[i]);
                            }
                        }
                        else {
                            if (isSelected) {
                                for (var n = index; n < idsArr.length - 1; n++) {
                                    idsArr[n] = idsArr[n + 1];
                                }
                                idsArr.length = idsArr.length - 1;
                            }
                        }
                    }
                    $("#selectedIds").val(idsArr.join(","));
                }
                else{
                    $("#selectedIds").val(aRowids.join(","));
                }
            },
            jsonReader: {
                root: "dataRows",
                page: "page",
                total: "total",
                records: "records",
                repeatitems : false
            },
            multiselect : true,
            rownumbers:true,
            gridview:true,
            shrinkToFit:true,
            viewrecords: true,
            autowidth: true,
            height:'auto',
            forceFit:true,
            loadComplete: function() {
            }
        });

        $(window).on('resize.jqGrid', function() {
            jQuery("#ajax_project_table").jqGrid('setGridWidth', $("#ajax_project_list_row").width());
        });
        jQuery("#ajax_project_table").jqGrid('filterToolbar',{searchOperators:false,stringResult:true});

        jQuery("#ajax_project_table").jqGrid('navGrid', "#ajax_project_list_page", {
            edit : false,
            add : false,
            del : false,
            search:false
        });

    };

    function load_projectFin_jqGrid(){
        jQuery("#ajax_projectFin_table").jqGrid({
            url:'ajax-project!listFin.action',
            datatype: "json",
            colNames:['项目名称','项目总监',"客户名称","客户联系人",'手机',"所在区域","项目地址","启动时间","完成时间","id"],
            colModel:[
                {name:'name',index:'name', width:120,sortable:true,search:true,
                    searchoptions:{sopt: ["cn"],clearSearch:true}},
                {name:'chief',index:'chief_name', width:80,search:true,
                    searchoptions:{sopt: ["cn"],clearSearch:true}},
                {name:'client',index:'client_name', width:200,search:true,
                    searchoptions:{sopt: ["cn"],clearSearch:true}},
                {name:'linkman',index:'linkman', width:80,search:false},
                {name:'mobile',index:'mobile', width:100,search:false},
                {name:'area',index:'area', width:120,search:false},
                {name:'address',index:'address', width:150,search:false},
                {name:'start',index:'start', width:80,search:false},
                {name:'end',index:'end', width:80,search:false},
                {name:'id',index:'id',hidden:true,search:false}
            ],
            rowNum : 10,
            rowList:[10,20,30],
            pager : '#ajax_projectFin_list_page',
            sortname : 'end',
            sortorder : "desc",
            gridComplete:function(){
                var ids=$("#ajax_projectFin_table").jqGrid('getDataIDs');
                var selectedIds = $("#selectedIds").val();
                for(var i=0;i<ids.length;i++){
                    var cl=ids[i];
//                    var objIds = $("#selectedIds").val();
//                    $(objIds.split(",")).each(function(i,v){
//                        if(v==cl)$("#ajax_projectFin_table").jqGrid('setSelection',v);
//                    });
                    if (selectedIds != "" && selectedIds != null){
                        var idsArr = selectedIds.split(",");
                        for (var s=0;s<idsArr.length;s++){
                            if (idsArr[s] == cl){
                                $("#ajax_projectFin_table").jqGrid("setSelection", cl);
                            }
                            // break;
                        }
                    }
                }
                $(".ui-jqgrid-bdiv").css("overflow-x","hidden");
                jqGridStyle();
            },
            onSelectRow: function (rowId, status, e) {
                var ids = $("#selectedIds").val();
                if (ids != ""){
                    var idsArr = ids.split(",");
                    var isSelected = false;
                    for (var i=0; i<idsArr.length;i++){
                        if (idsArr[i] == rowId){
                            isSelected = true;
                            break;
                        }
                    }
                    if (status){
                        if (!isSelected)
                            $("#selectedIds").val(ids + "," +rowId);
                    }
                    else{
                        if (isSelected) {
                            if (idsArr.length < 2){
                                $("#selectedIds").val("");
                            }
                            else{
                                for (var i = 0; i < idsArr.length; i++) {
                                    if (idsArr[i] == rowId) {
                                        for (var j = i; j < idsArr.length - 1; j++) {
                                            idsArr[j] = idsArr[j + 1];
                                        }
                                        idsArr.length = idsArr.length - 1;
                                        break;
                                    }
                                }
                                $("#selectedIds").val(idsArr.join(","));
                            }
                        }
                    }
                }
                else{
                    $("#selectedIds").val(rowId);
                }
            },
            onSelectAll:function(aRowids, status, e){
                //alert(aRowids);
                var ids = $("#selectedIds").val();
                if (ids != ""){
                    var idsArr = ids.split(",");
                    for (var i=0;i<aRowids.length;i++){
                        var isSelected = false;
                        var index = -1;
                        for (var j=0;j<idsArr.length;j++){
                            if (aRowids[i] == idsArr[j]){
                                isSelected = true;
                                index = j;
                                break;
                            }
                        }
                        if (status){
                            if (!isSelected){
                                idsArr.push(aRowids[i]);
                            }
                        }
                        else{
                            if (isSelected){
                                for (var n = index;n<idsArr.length-1;n++){
                                    idsArr[n] = idsArr[n + 1];
                                }
                                idsArr.length = idsArr.length - 1;
                            }
                        }
                    }
                    $("#selectedIds").val(idsArr.join(","));
                }
            },
            jsonReader: {
                root: "dataRows",
                page: "page",
                total: "total",
                records: "records",
                repeatitems : false
            },
            multiselect : true,
            rownumbers:true,
            gridview:true,
            shrinkToFit:true,
            viewrecords: true,
            autowidth: true,
            height:'auto',
            forceFit:true,
            loadComplete: function() {
            }
        });
        $(window).on('resize.jqGrid', function() {
            jQuery("#ajax_projectFin_table").jqGrid('setGridWidth', $("#ajax_projectFin_list_row").width());
        });
        jQuery("#ajax_projectFin_table").jqGrid('filterToolbar',{searchOperators:false,stringResult:true});
        jQuery("#ajax_projectFin_table").jqGrid('navGrid', "#ajax_projectFin_list_page", {
            edit : false,
            add : false,
            del : false,
            search:false
        });
    }
    function fn_project_read(id){
        loadURL("ajax-project!read.action?keyId="+id,$('#business_data'));
    }

    function fn_project_destroy(id){
        $.SmartMessageBox({
            title : "操作提示：",
            content : "确定要删除项目信息吗？",
            buttons : '[取消][确认]'
        }, function(ButtonPressed) {
            if (ButtonPressed === "取消") {
                //e.preventDefault();
                //e.stopPropagation();
                return;
            }
            if (ButtonPressed === "确认") {
                var vActionUrl = "<%=path%>/com/ajax-project!delete.action";
                data={keyId:id};
                ajax_action(vActionUrl,data,{},function(pdata){
                    _show(pdata);
                });
                jQuery("#ajax_project_table").trigger("reloadGrid");
            }
        });
    }

    function fn_project_finish(id){
        $.SmartMessageBox({
            title : "操作提示：",
            content : "确定项目已经完成了吗？",
            buttons : '[取消][确认]'
        }, function(ButtonPressed) {
            if (ButtonPressed === "取消") {
                //e.preventDefault();
                //e.stopPropagation();
                return;
            }
            if (ButtonPressed === "确认") {
                var vActionUrl = "<%=path%>/com/ajax-project!finish.action";
                data={keyId:id};
                ajax_action(vActionUrl,data,{},function(pdata){
                    _show(pdata);
                });
                jQuery("#ajax_project_table").trigger("reloadGrid");
            }
        });
    }

    $("a#finish").off("click").on("click",function(e){
        load_projectFin_jqGrid();
        var width = $("#ajax_project_list_row").width();
        $("#ajax_projectFin_list_row").width(width);
        jQuery("#ajax_projectFin_table").jqGrid('setGridWidth', width);
        $(this).tab('show');
        return false;
    });
</script>

<script type="text/javascript">
    $("#ajax_export_proInfo_datas").click(function(){
       // alert($("#selectedIds").val());
        if($("#selectedIds").val()==""){
            var data = {
                state:400,
                title:"操作状态",
                message:"请先勾选需要导出的项目信息"
            }
            _show(data);
            return;
        }

        $.SmartMessageBox({
            title : "确定导出项目信息数据吗？",
            content : "确定导出操作将进入创建导出文件队列，进入下载中心可查看导出文件执行情况。",
            buttons : '[取消][确认]'
        }, function(ButtonPressed) {
            if (ButtonPressed === "确认") {
                var idList=$("input#selectedIds").val();
                location.href = "ajax-import!exportData.action?keyId=proInfo&idList="+idList;
                /*
                ajax_action("ajax-import!exportData.action?keyId=proInfo"+"&idList="+idList,null,{},function(pdata){
                    _show(pdata);
                });*/
            }
        });

    });

    $("#ajax_download_proInfo_datas").click(function(){
        loadURL("ajax!downloadFile.action",$("#content"));
    });
</script>